Page({
    data:{
        nowPosttion:"",
        nowPosition_id:"",
        hasSeat:false,
        myAppointment:false
    },
    refresh() { //每次拉取座位就吧第一个座位的所有座位信息拉去出来
        wx.cloud.callFunction({
            name: 'function',
            data: {
                type: "getPosition"
            }
        }).then(res => {
            if (res.result.data.length > 0) {
                this.setData({
                    position: res.result.data,
                    hasSeat: true,
                    nowPosttion: res.result.data[0].position,
                    nowPosition_id: res.result.data[0]._id
                })
                wx.cloud.callFunction({
                    name: 'function',
                    data: {
                        type: "getSeat",
                        position_id: this.data.nowPosition_id
                    }
                }).then(res => {
                    var res2 = res.result.data
                    for (var i = 0; i < res2.length; i++) {
                        var appointmentTimeOut = false
                        if (new Date(res.result.data[0].checkTime) < new Date()) {
                            //checkTime是签到的最后期限
                            //如果比现在时间大，则已经预约，还没签到
                            //签到最后时间比现在时间小，则可能已经预约，签到超时，评判的总前提是check=true
                            appointmentTimeOut = true
                        }
                        if (res2[i].check) {
                            res2[i].show = true
                        } else {
                            if (res2[i].isAppointment) {
                                if (appointmentTimeOut) {
                                    res2[i].show = false
                                } else {
                                    res2[i].show = true
                                }
                            } else {
                                res2[i].show = false
                            }
                        }
                    }
                    this.setData({
                        seats: res2
                    })
                })
            } else {
                this.setData({
                    hasSeat: false
                })
            }
        })
        wx.cloud.callFunction({
            name: 'function',
            data: {
                type: "checkMyAppointment"
            }
        }).then(res => {
            var result = res.result.data;
            var isMeHaveAppointment = false
            for (var i = 0; i < result.length; i++) {
                if (result[i].check) {
                    isMeHaveAppointment = true;
                    break
                } else {
                    if (result[i].isAppointment) {
                        if (new Date(result[0].checkTime) > new Date()) {
                            isMeHaveAppointment = true;
                            break
                        } else {
                            isMeHaveAppointment = false;
                        }
                    }else{
                        isMeHaveAppointment = false
                    }
                }
            }
            this.setData({
                myAppointment:isMeHaveAppointment
            })
        })
    },
    onLoad: function (options) {
        
    },
    onShow(){
        this.refresh()
    },
    tab_clisk(e) {
        this.setData({
            nowPosttion: this.data.position[e.target.dataset.idx].position,
            nowPosition_id: this.data.position[e.target.dataset.idx]._id,
        })
        wx.cloud.callFunction({
            name: 'function',
            data: {
                type: "getSeat",
                position_id: this.data.nowPosition_id
            }
        }).then(res => {
            if (res.result.data.length > 0) {
                this.setData({
                    seats: res.result.data,
                    hasSeat: true
                })
            } else {
                this.setData({
                    hasSeat: false
                })
            }
        })
    },
    seatTap(e){
        var seat = e.currentTarget.dataset.itemname
        var that = this
        if (!seat.show){
            if(this.data.myAppointment){
                wx.showToast({title: '您有预约'})
            }else{
                wx.showModal({
                    title: '提示',
                    confirmText: "预约",
                    content: "预约后您有10分钟的时间到座位二维码签到，是否预约?",
                    success(e) {
                        if (e.confirm) {
                            wx.cloud.callFunction({
                                name: 'function',
                                data: {
                                    type: "appointment",
                                    _id: seat._id,
                                    position_id: seat.position_id
                                }
                            }).then(res => {
                                that.refresh()
                                wx.navigateTo({
                                    url: "./myappointment/myappointment"
                                })
                            })
                        }
                    }
                })
            }
        } else {
            wx.showToast({
                title: '已被预约',
            })
        }
    },
    onShareAppMessage() {

    },
    onShareTimeline() {

    }
})